#include "MenuManager.h"
#include "RenderManager.h"
#include "..\Engines\RenderEngine.h"
#include "..\Managers\InputManager.h"
#include "..\GUI\Button.h"

MenuManager::MenuManager(const vn::Vector2& p_position, float p_spacing) : m_position(p_position), m_spacing(p_spacing), m_currentChoice(0), m_isActive(true), m_title(nullptr) {}

MenuManager::~MenuManager(void)
{
	if (!m_choices.empty())
	{
		for (auto it = m_choices.begin(); it != m_choices.end(); it++)
		{
			delete (*it);
			(*it) = nullptr;
		}
		m_choices.clear();
	}
	if (m_title != nullptr)
	{
		RenderManager::getSingleton().getRenderEngine()->destroyText(m_title);
	}
}

void MenuManager::update()
{
	if (m_isActive)
	{
		int oldSelection = m_currentChoice;
		if (InputManager::getSingleton().isKeyPressed("up"))
		{
			m_currentChoice--;
			confirmPosition();
			confirmSelectionColor(oldSelection);
		}
		else if (InputManager::getSingleton().isKeyPressed("down"))
		{
			m_currentChoice++;
			confirmPosition();
			confirmSelectionColor(oldSelection);
		}

		if (InputManager::getSingleton().isKeyPressed("jump"))
		{
			m_choices[m_currentChoice]->call();
		}

		for (auto it = m_choices.begin(); it != m_choices.end(); it++)
			(*it)->update();
	}
}

void MenuManager::addChoice(Button* p_button)
{
	if (!m_choices.empty())
	{
		float newY = m_choices.back()->getPosition().y + m_spacing;
		p_button->setPosition(vn::Vector2(m_position.x, newY));
	}
	else
	{
		p_button->setPosition(m_position);
	}
	m_choices.push_back(p_button);
}

void MenuManager::setSpacing(float p_spacing)
{

}

void MenuManager::setPosition(const vn::Vector2& p_position)
{
	// TODO
}

void MenuManager::resetTextPositions()
{
	// TODO
}

inline void MenuManager::confirmPosition()
{
	if (m_currentChoice < 0)
	{
		m_currentChoice = static_cast<int>(m_choices.size() - 1);
	}
	else if (m_currentChoice > static_cast<int>(m_choices.size() - 1))
	{
		m_currentChoice = 0;
	}
}


void MenuManager::setCurrentChoice(int p_choice)
{
	int oldSelection = m_currentChoice;
	m_currentChoice = p_choice;
	confirmPosition();
	confirmSelectionColor(oldSelection);
}

void MenuManager::confirmSelectionColor(int p_oldSelection)
{
	m_choices[p_oldSelection]->setActive(false);
	m_choices[m_currentChoice]->setActive(true);
}

void MenuManager::setActive(bool p_value)
{
	m_isActive = p_value;
	if (!m_choices.empty())
	{
		for (auto it = m_choices.begin(); it != m_choices.end(); it++)
		{
			(*it)->setDoRender(p_value);
		}
	}
	if (m_title != nullptr)
	{
		m_title->setDoRender(p_value);
	}
}

void MenuManager::addTitle(unsigned int p_view, const std::string& p_string, const std::string& p_font, const vn::Color& p_color, float p_scale, float p_layer)
{
	if (m_title != nullptr)
	{
		RenderManager::getSingleton().getRenderEngine()->destroyText(m_title);
	}
	m_title = RenderManager::getSingleton().getRenderEngine()->createText(p_view, p_font, p_string, p_layer);
	m_title->setColor(p_color);
	m_title->setScale(p_scale, p_scale);
	m_title->setOrigin(m_title->getLocalBounds().w * 0.5f, m_title->getLocalBounds().h * 0.5f);
	m_title->setPosition(m_position.x, m_position.y - m_spacing);
}